using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;

namespace TrenitaliaWP7
{
    public class SavedState
    {
        public List<Train> trains;
        public List<Result> results;
        public Details chosen;
        public int current_result;
        public string from;
        public string to;
        public DateTime dateTime;
        public bool isPrevMainMenu;
        public bool openedFavourites;
        public int rows_offset;
        public bool searchAllSolutions;
        public string originalQuery;
        public string originalBuyQuery;
        public SavedState()
        {
            trains = new List<Train>();
            results = new List<Result>();
            chosen = new Details();
            current_result = 0;
            from = "";
            to = "";
            dateTime = new DateTime();
            openedFavourites = false;
            rows_offset = 0;
            searchAllSolutions = false;
            originalQuery = "";
            originalBuyQuery = "";
        }


        public override string ToString()
        {


            //var writer = new StringWriter(s);
            //    //write_options_state(writer, entry);
                
            //XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
            //serializer.Serialize(writer, this);
    
                StringWriter sw = new StringWriter();
     
                XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
                serializer.Serialize(sw, this);
     
                return sw.ToString();
    
        }
    }


    public static class State
    {


        #region saved_state
        public static SavedState LoadSavedState()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var streamFile = appStorage.OpenFile("savedstate.xml", FileMode.OpenOrCreate))
                {
                    try
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
                        using (var reader = new StreamReader(streamFile))
                            return serializer.Deserialize(reader) as SavedState;
                    }
                    catch (Exception) { return new SavedState(); }
                }
            }
        }

        public static void DeleteSavedState()
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("savedstate.xml");
            }
        }

        public static void SaveSavedState(SavedState state)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("savedstate.xml");
                using (var streamFile = appStorage.OpenFile("savedstate.xml", FileMode.OpenOrCreate))
                using (var writer = new StreamWriter(streamFile))
                //write_options_state(writer, entry);
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SavedState));
                    serializer.Serialize(writer, state);
                }
            }
        }
        #endregion
    }
}
